package com.ruoyi.web.controller.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.controller.system.domain.Orders;
import com.ruoyi.web.controller.system.mapper.OrdersMapper;
import com.ruoyi.web.controller.system.service.chartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import oshi.util.FormatUtil;

import java.util.*;

/**
 * @author Emma Li
 * @date 2020/12/23 - 16:45
 */
@Service
public class chartServiceImpl implements chartService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public Map<String, Object> getData() {

        String date = DateUtils.getDate();
        List<Orders> orders = ordersMapper.selectOrdersList(new Orders());
        List<Orders> list = new ArrayList<>();
        List<String> day = new ArrayList<>();

        for(Orders order:orders){
            String createdAt = order.getCreatedAt();
//            String[] s = createdAt.split(" ");

            if(createdAt.compareTo(date)<0){//存入包括今天以前的所有订单
                list.add(order);
            }
        }

        int[] price = new int[30];
        int[] sum=new int[30];

//        HashMap<String, Integer> priceMap = new HashMap<>();

        for(Orders order:list){
            String createdAt = order.getCreatedAt();
            String[] s = createdAt.split(" ");
            if(!day.contains(s[0])){//新日期
                day.add(s[0]);
                int i = day.indexOf(s[0]);
                price[i]= Math.toIntExact(order.getTotalFee());
                sum[i]++;
            }
            else{
                int i = day.indexOf(s[0]);
                price[i]+= Math.toIntExact(order.getTotalFee());
                sum[i]++;
            }
        }

        List<String> p = new ArrayList<>();
        List<String> s = new ArrayList<>();

        for(int i=0;i<price.length;i++){
            p.add(String.valueOf(price[i]));
        }
        for(int i=0;i<sum.length;i++){
            s.add(String.valueOf(sum[i]));
        }


        HashMap<String, Object> map = new HashMap<>();
        map.put("price",p);
        map.put("sum",s);
        map.put("day",day);
        return map;
    }
}
